package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.SetmealCheckgroupDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealCheckgroupDao setmealCheckgroupDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 套餐分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.pageQuery(queryPageBean.getQueryString());
        long total = page.getTotal();
        List<Setmeal> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Transactional
    public void save(Setmeal setmeal, String checkgroupIds) {
        setmealDao.save(setmeal);
        if(checkgroupIds != null && !checkgroupIds.equals("")){
            String[] split = checkgroupIds.split(",");
            for (String s : split) {
                setmealCheckgroupDao.insert(setmeal.getId(), Integer.parseInt(s));
            }
        }
        if(setmeal.getImg() != null && setmeal.getImg() != ""){
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        }

    }

    /**
     * 查询套餐
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    /**
     * 查询套餐详细数据
     * @param id
     * @return
     */
    public Setmeal findByIdDetails(Integer id) {
        return setmealDao.findByIdDetails(id);
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     */
    @Transactional
    public void update(Setmeal setmeal, String checkGroupIds) {
        String img = setmealDao.findImgById(setmeal.getId());

        setmealDao.update(setmeal);

        Integer ids = setmealCheckgroupDao.getBySetmealId(setmeal.getId());
        if(ids > 0){
            setmealCheckgroupDao.deleteBySetmealId(setmeal.getId());
        }
        if(checkGroupIds != null && !checkGroupIds.equals("")){
            String[] split = checkGroupIds.split(",");
            for (String s : split) {
                setmealCheckgroupDao.insert(setmeal.getId(), Integer.parseInt(s));
            }
        }
        if(img != null && img != ""){
            redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_DB_RESOURCES, img);
        }
        if(setmeal.getImg() != null && setmeal.getImg() != ""){
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        }

    }

    /**
     * 删除套餐
     * @param id
     */
    @Transactional
    public void deleteById(Integer id) {
        Integer count = setmealCheckgroupDao.getBySetmealId(id);
        if(count > 0){
            setmealCheckgroupDao.deleteBySetmealId(id);
        }
        setmealDao.delete(id);
    }

    /**
     * 套餐列表全部数据
     */
    public List<Setmeal> getSetmeal() {
        return setmealDao.getSetmeal();
    }

}
